﻿// <copyright file="dates.cs" company="Microsoft Corporation">
// Copyright (c) 2008 All Right Reserved
// </copyright>
// <author>Michael S. Scherotter</author>
// <email>mischero@microsoft.com</email>
// <date>2009-03-03</date>
// <summary>Dates class</summary>

namespace NYTimes
{
    using System;
    using System.ComponentModel;
    using System.Diagnostics.CodeAnalysis;
    using System.IO.IsolatedStorage;

    /// <summary>
    /// Dates CLR class.
    /// </summary>
    public class Dates : INotifyPropertyChanged
    {
        /// <summary>
        /// Key for best sellers weeks in isolated storage
        /// </summary>
        private const string BestSellerWeeks = "BestSellerWeeks";

        /// <summary>
        /// default number of weeks
        /// </summary>
        private double weeks = 8;

        /// <summary>
        /// Initializes a new instance of the Dates class.
        /// </summary>
        [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "This might fail in Blend.")]
        public Dates()
        {
        }

        #region INotifyPropertyChanged Members

        /// <summary>
        /// the property changed event handler
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        /// <summary>
        /// Gets the time now
        /// </summary>
        [SuppressMessage("Microsoft.Performance", "CA1822", Justification = "This is exposed to a Silverlight application.")]
        public DateTime Now
        {
            get
            {
                return DateTime.Now;
            }
        }

        /// <summary>
        /// Gets the OffsetDate
        /// </summary>
        public DateTime OffsetDate
        {
            get
            {
                var now = this.Now;

                now = now.AddDays(this.Weeks * -7.0);

                return now;
            }
        }

        /// <summary>
        /// Gets or sets the offset days
        /// </summary>
        [Description("The number of weeks to offset.")]
        public double Weeks
        {
            get
            {
                return this.weeks;
            }

            set
            {
                if (this.weeks != value)
                {
                    this.weeks = value;

                    if (this.PropertyChanged != null)
                    {
                        this.PropertyChanged(this, new PropertyChangedEventArgs("Weeks"));
                        this.PropertyChanged(this, new PropertyChangedEventArgs("OffsetDate"));
                    }
                }
            }
        }
    }
}
